package com.example.zaq.rest;

import com.example.zaq.dao.AspCalendarDao;
import com.example.zaq.model.AspCalendar;
import com.example.zaq.model.AspSelectMonth;
import com.example.zaq.model.Summary;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 阿恩
 * 20210601  zaq  引入
 * 20210602  zaq  实现增删改查方法
 * 20210603  zaq  实现按天查询日程
 * 20210604  zaq  查询本月收支、说说、日程数量信息图表
 * @date 2021/6/121:05
 */
@RestController
@RequestMapping("/calendar")
public class AspCalendarRest {
    @Autowired
    SqlSessionFactory factory;

    /**
     * 查询本月收支、说说、日程数量信息图表
     * http://localhost:9090/calendar/month
     * @param body
     * @return
     */
    @PostMapping("month")
    @ResponseBody
    public List<AspSelectMonth> month(@RequestBody AspCalendar body){
        List<AspSelectMonth> aspCalendar=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspCalendarDao aspCalendarDao=sess.getMapper(AspCalendarDao.class);
            aspCalendar=aspCalendarDao.select(body);
            sess.close();
            System.out.println(aspCalendar);
            System.out.println("查询结束");
            return aspCalendar;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspCalendar;
        }
    }

    /**
     * 按天查询日程
     * http://localhost:9090/calendar/selectDay
     * @param body
     * @return
     */
    @PostMapping("selectDay")
    @ResponseBody
    public List<AspCalendar> getDay(@RequestBody AspCalendar body){
        List<AspCalendar> aspCalendar=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspCalendarDao aspCalendarDao=sess.getMapper(AspCalendarDao.class);
            aspCalendar=aspCalendarDao.selectDay(body);
            sess.close();
            System.out.println(aspCalendar);
            System.out.println("查询日程结束");
            return aspCalendar;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspCalendar;
        }
    }

    /**
     * 按查询月日程图表
     * http://localhost:9090/calendar/selectSummary
     * @param body
     * @return
     */
    @PostMapping("selectSummary")
    @ResponseBody
    public List<Summary> getSummary(@RequestBody AspCalendar body){
        List<Summary> aspCalendar=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspCalendarDao aspCalendarDao=sess.getMapper(AspCalendarDao.class);
            aspCalendar=aspCalendarDao.selectSummary(body);
            sess.close();
            for(int i=0;i<aspCalendar.size();i++){
                Summary s=aspCalendar.get(i);
                //日期格式化
                StringBuilder sb = new StringBuilder(s.getName());
                sb.insert(6,'-');
                sb.insert(4,'-');
                s.setName(sb.toString());
                aspCalendar.set(i,s);
            }
            System.out.println(aspCalendar);
            System.out.println("查询日程结束");
            return aspCalendar;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspCalendar;
        }
    }

    /**
     * 修改一条日程
     * http://localhost:9090/calendar/update
     * @param body
     * @return
     */
    @PostMapping("update")
    @ResponseBody
    public AspCalendar update(@RequestBody AspCalendar body){
        AspCalendar aspCalendar=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspCalendarDao aspCalendarDao=sess.getMapper(AspCalendarDao.class);
            aspCalendar=aspCalendarDao.update(body);
            sess.close();
            System.out.println(aspCalendar);
            System.out.println("修改日程结束");
            return aspCalendar;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspCalendar;
        }
    }

    /**
     * 删除一条日程
     * http://localhost:9090/calendar/delete
     * @param body
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public AspCalendar delete(@RequestBody AspCalendar body){
        AspCalendar aspCalendar=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspCalendarDao aspCalendarDao=sess.getMapper(AspCalendarDao.class);
            aspCalendar=aspCalendarDao.delete(body);
            sess.close();
            System.out.println(aspCalendar);
            System.out.println("删除日程结束");
            return aspCalendar;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspCalendar;
        }
    }

    /**
     * 新增一条日程
     * http://localhost:9090/calendar/insert
     * @param body
     * @return
     */
    @PostMapping("insert")
    @ResponseBody
    public AspCalendar insert(@RequestBody AspCalendar body){
        AspCalendar aspCalendar=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspCalendarDao aspCalendarDao=sess.getMapper(AspCalendarDao.class);
            aspCalendar=aspCalendarDao.insert(body);
            sess.close();
            System.out.println(aspCalendar);
            System.out.println("新增日程结束");
            return aspCalendar;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspCalendar;
        }
    }

    /**
     * 按查询一条日程
     * http://localhost:9090/calendar/selectOne
     * @param body
     * @return
     */
    @PostMapping("selectOne")
    @ResponseBody
    public AspCalendar getOne(@RequestBody AspCalendar body){
        AspCalendar aspCalendar=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspCalendarDao aspCalendarDao=sess.getMapper(AspCalendarDao.class);
            aspCalendar=aspCalendarDao.selectByPrimaryKey(body.getcId());
            sess.close();
            System.out.println(aspCalendar);
            System.out.println("查询日程结束");
            return aspCalendar;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspCalendar;
        }
    }

    /**
     * 按月查询日程
     * http://localhost:9090/calendar/selectMonth
     * @param body
     * @return
     */
    @PostMapping("selectMonth")
    @ResponseBody
    public List<AspCalendar> getAll(@RequestBody AspCalendar body){
        List<AspCalendar> aspCalendar=null;
        try{
            System.out.println("获得值:"+body);
            SqlSession sess=factory.openSession();
            AspCalendarDao aspCalendarDao=sess.getMapper(AspCalendarDao.class);
            aspCalendar=aspCalendarDao.selectMonth(body);
            sess.close();
            System.out.println(aspCalendar);
            System.out.println("查询日程结束");
            return aspCalendar;
        }catch (Exception e){
            System.out.println(e.getMessage());
        }finally {
            return aspCalendar;
        }
    }
}
